import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LKQ
 * @date 2022/2/21 16:13
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.customSortString("cbafg", "abcd");
    }
    public String customSortString(String order, String s) {
        int n =s.length();
        char[][] arr = new char[n][2];
        Map<Character, Character> map = new HashMap<>();
        for (int i = 0; i < order.length(); i++) {
            map.put(order.charAt(i), (char) (i+'1'));
        }
        for (int i = 0; i < n; i++) {
            arr[i][0] = s.charAt(i);
            arr[i][1] = map.get(s.charAt(i)) == null ? '0' : map.get(s.charAt(i));
        }
        Arrays.sort(arr, (o1, o2) -> {
            if (o1[1] == '0' && o2[1] == '0') {
                return o1[0] - o2[0];
            }else {
                return o1[1] - o2[1];
            }
        });
        char[] ans = new char[n];
        for (int i = 0; i < n; i++) {
            ans[i] = arr[i][0];
        }
        return new String(ans);
    }
}
